//
//  AMSearchView.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOSearchView.h"
#import "HOUtils.h"
#import "Logger.h"

@implementation HOSearchView
@synthesize action;
@synthesize searchStatus;
@synthesize delegate;
@synthesize dataList;


- (instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
		//[self initBgView];
		[self initNoDataUI];
		[self initSearchBar];
		

    }
    return self;
}

- (void)initBgView
{
	
	bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	[self addSubview:bgView];
}
- (void)initNoDataUI
{
	noResultImgView = [[UIImageView alloc]
					   initWithFrame:CGRectMake((self.frame.size.width - 100) / 2,
												(self.frame.size.height - 100) / 2,
												100,
												100)];
	[self addSubview:noResultImgView];
}

- (void)initSearchBar
{
	
	searchBar = [[UISearchBar alloc]
				 initWithFrame:CGRectMake(0,
										  0,
										  self.frame.size.width,
										  44)];
	
	[searchBar setBackgroundImage:[HOUtils createImageWithColor:MAIN_BG_COLOR forView:searchBar]
				   forBarPosition:0
					   barMetrics:UIBarMetricsDefault];
	searchBar.delegate = self;
	searchBar.placeholder = NSLocalizedString(@"输入关键字", @"输入关键字");
	[self addSubview:searchBar];
	searchBar.showsCancelButton = YES;
	//        searchBar.showsSearchResultsButton = YES;
	//        searchBar.showsScopeBar = YES;
	
	searchTableView = [[UITableView alloc] init];
	//        searchTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
	searchTableView.delaysContentTouches = NO;
	searchTableView.frame = CGRectMake(0,
									   searchBar.frame.size.height + searchBar.frame.origin.y,
									   self.frame.size.width,
									   self.frame.size.height - searchBar.frame.size.height);
	//searchTableView.layer.borderWidth = 3;
	[searchTableView setBackgroundColor:MAIN_BG_COLOR];
	dataList = [[NSMutableArray alloc]init];
	[self addSubview:searchTableView];
	[searchBar setBackgroundColor:[UIColor clearColor]];
	//[self updateSearchBar];
	searchBar.tintColor = MAIN_COLOR_NORMAL;
}

- (void)updateSearchBar
{
	for (UIView *v in searchBar.subviews)
	{
		for(id subview in v.subviews)
		{
			if ([subview isKindOfClass:[UITextField class]])
			{
				((UITextField *)subview).font = [HOUtils fontWithSize:14];
				[((UITextField *)subview) setBackgroundColor:[UIColor colorWithRed:255 green:255 blue:255 alpha:0.3]];
				
				[ ((UITextField *)subview) setValue:UIColorFromRGB(0xffffff) forKeyPath:@"_placeholderLabel.textColor"];//新加代码，修改placeholder文字颜色，上截图时没这效果，测试可行
				
				//					((UITextField *)subview).delegate = self;
				
				[((UITextField *)subview) setBorderStyle:UITextBorderStyleRoundedRect];
				UIImage *image = [UIImage imageNamed: @"icon_search"];
				UIImageView *iView = [[UIImageView alloc] initWithImage:image];
				((UITextField *)subview).leftView = iView;
				[iView release];
				
				
			}
		}
	}
}

- (UISearchBar *)getSearchBar
{
    return searchBar;
}

- (void)setTableViewDataSource:(id<UITableViewDataSource>)tableViewDataSourceDelegate
{
    searchTableView.dataSource = tableViewDataSourceDelegate;
}

- (void)setTableViewDelegate:(id<UITableViewDelegate>)tableViewDelegate;
{
    searchTableView.delegate = tableViewDelegate;
}
- (void)openSearchView
{
    self.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 1;
    }completion:^(BOOL finished)
     {
        self.alpha = 1;
         searchStatus = HO_SEARCH_OPENED;
         [self statusDidChange];
     }];
    [searchBar becomeFirstResponder];
}
- (void)closeSearchView
{
   
    [UIView animateWithDuration:0.5 animations:^{
        self.alpha = 0;
        self.hidden = YES;
        searchStatus = HO_SEARCH_CLOSED;
        [self statusDidChange];
    }completion:^(BOOL finished)
     {
         self.alpha = 0;
         self.hidden = YES;
         searchStatus = HO_SEARCH_CLOSED;
         [self.dataList removeAllObjects];
         [searchTableView reloadData];
         searchBar.text = @"";
         [self statusDidChange];
     }];
    [searchBar resignFirstResponder];
}
- (void)statusDidChange
{
    [self updateUI];
//    LogInf(@"statusDidChange search status:%d", self.searchStatus);
    if([self.delegate respondsToSelector:@selector(searchStatusDidChange:)])
    {
        [self.delegate searchStatusDidChange:self.searchStatus];
    }
}
- (void)updateUI
{
    if(self.searchStatus == HO_SEARCH_OPENED || self.searchStatus == HO_SEARCH_CLOSED)
    {
        [bgView setBackgroundColor:[UIColor blackColor]];
        bgView.alpha = 0.5;
        searchTableView.hidden = YES;
        noResultImgView.hidden = YES;
    }
    else if(self.searchStatus == HO_SEARCH_NORESULt)
    {
        bgView.alpha = 1;
        [bgView setBackgroundColor:[UIColor whiteColor]];
        noResultImgView.hidden = NO;
        searchTableView.hidden = YES;
    }
    else if(self.searchStatus == HO_SEARCH_SEARCHED)
    {
        bgView.alpha = 1;
        [bgView setBackgroundColor:[UIColor whiteColor]];
        noResultImgView.hidden = YES;
        searchTableView.hidden = NO;
//        searchTableView.layer.borderWidth = 3;
    }
    else if(self.searchStatus == HO_SEARCH_SEARCHING)
    {
        [bgView setBackgroundColor:[UIColor blackColor]];
        bgView.alpha = .5;
        noResultImgView.hidden = YES;
        searchTableView.hidden = YES;
    }
    
}
- (UIView *)getBgView
{
    return bgView;
}

#pragma mark - -- UISearchBarDelegate impelemnt methods
// called when keyboard search button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar
{
    searchStatus = HO_SEARCH_SEARCHING;
    [self statusDidChange];
    NSString *key = [[aSearchBar text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if([self.delegate respondsToSelector:@selector(gotoSearchKey:)])
    {
//        LogInf(@"key:%@", key);
        [self.delegate gotoSearchKey:key];
    }
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
//    [self closeSearchView];
    [self statusDidChange];
}

- (UITableView *)getTableView
{
    return searchTableView;
}
#pragma mark - -- didReceiveResponse

- (void)reloadData
{
    searchStatus = ([self.dataList count] == 0 ) ? HO_SEARCH_NORESULt : HO_SEARCH_SEARCHED;
    [self statusDidChange];
    [searchTableView reloadData];
    [searchBar resignFirstResponder];
    
}
#pragma mark - -- dealloc

- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:searchBar, searchTableView, bgView];
    [searchBar release];
    [searchTableView release];
    [bgView release];
    [super dealloc];
}
@end
